package com.quwan.manager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quwan.common.query.LambdaQueryWrapperX;
import com.quwan.common.result.PageResult;
import com.quwan.config.yml.OssConfig;
import com.quwan.constans.CacheKey;
import com.quwan.entity.TBanner;
import com.quwan.manager.service.BannerSystemService;
import com.quwan.mapper.TBannerMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


/**
 * @author laozhang
 * @date 2021/9/25 11:54
 */
@Service
@RequiredArgsConstructor
public class BannerSystemServiceImpl implements BannerSystemService {

    private final TBannerMapper bannerMapper;
    private final OssConfig ossConfig;

    @Override
    public PageResult<TBanner> getBannerListPage(int page, int size, String title) {
        LambdaQueryWrapperX<TBanner> wrapper = new LambdaQueryWrapperX<TBanner>()
                .orderByDesc(TBanner::getCreateTime);
        if (StringUtils.isNotBlank(title)) {
            wrapper.like(TBanner::getTitle, title);
        }

        Page<TBanner> bannerPage = bannerMapper.selectPage(new Page<>(page, size), wrapper);
        List<TBanner> reslist = bannerPage.getRecords();
        /* 开始遍历 */
        for (TBanner ban : reslist) {
            if (ban.getImageUrl() != null && ban.getImageUrl().length() > 0) {
                ban.setImageUrl(ossConfig.getHostname() + "/" + ban.getImageUrl());
            }
        }
        PageResult<TBanner> result = new PageResult<TBanner>()
                .setCurrent(bannerPage.getCurrent())
                .setPages(bannerPage.getPages())
                .setSize(bannerPage.getSize())
                .setTotal(bannerPage.getTotal())
                .setRecords(reslist);
        return result;
    }

    @Override
    public List<TBanner> getBannerList(String title) {
        LambdaQueryWrapperX<TBanner> wrapper = new LambdaQueryWrapperX<TBanner>()
                .orderByDesc(TBanner::getCreateTime);
        if (StringUtils.isNotBlank(title)) {
            wrapper.like(TBanner::getTitle, title);
        }
        List<TBanner> res = bannerMapper.selectList(wrapper);
        /* 开始遍历 */
        for (TBanner ban : res) {
            if (ban.getImageUrl() != null && ban.getImageUrl().length() > 0) {
                ban.setImageUrl(ossConfig.getHostname() + "/" + ban.getImageUrl());
            }
        }
        return res;
    }

    @CacheEvict(cacheNames =CacheKey.DATA.banner, allEntries = true)
    @Override
    public boolean addBanner(TBanner banner) {
        return bannerMapper.insert(banner.setCreateTime(LocalDateTime.now())) == 1;
    }

    @CacheEvict(cacheNames =CacheKey.DATA.banner, allEntries = true)
    @Override
    public boolean updateBanner(TBanner banner) {
        return bannerMapper.updateById(banner) == 1;
    }

    @Override
    public TBanner detailBanner(Integer id) {
        TBanner ban = bannerMapper.selectById(id);
        if (ban.getImageUrl().length() > 0) {
            ban.setImageUrl(ossConfig.getHostname() + "/" + ban.getImageUrl());
        }
        return ban;
    }

    @CacheEvict(cacheNames =CacheKey.DATA.banner, allEntries = true)
    @Override
    public boolean delBanner(TBanner banner) {
        return bannerMapper.deleteById(banner.getId()) == 1;
    }

    @CacheEvict(cacheNames =CacheKey.DATA.banner, allEntries = true)
    @Override
    public boolean changeStatus(TBanner banner) {
        return bannerMapper.updateById(banner) == 1;

    }

}
